import os from 'node:os';

/** 获取本机ip */
export const getLocalIp = (): string => {
  let ip: string = '';
  const osType = os.type(); //系统类型
  const netInfo = os.networkInterfaces(); //网络信息
  // windows 取 Ipv4
  if (osType === 'Windows_NT') {
    Object.keys(netInfo).forEach((item) => {
      netInfo[item].forEach((info) => {
        const { family, address, internal } = info;
        if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {
          ip = address;
        }
      });
    });
  } else if (['Linux', 'Drawin'].includes(osType)) {
    // linux mac
    ip = netInfo.eth0[0].address;
  }

  return ip;
};
